/**=========================================================
 * Service: route-helpers.js
 * Provides helper functions for routes definition
 =========================================================*/

App.provider('RouteHelpers', ['APP_REQUIRES', function (appRequires) {
    "use strict";

    // Set here the base of the relative path
    // for all app views
    this.basepath = function(uri) {
        return 'app/views/' + uri + "?_t=" + (new Date()).valueOf();
    };

    // Generates a resolve object by passing script names
    // previously configured in constant.APP_REQUIRES
    this.resolveFor = function() {
        var _args = arguments;
        return {
        deps: ['$ocLazyLoad','$q', function ($ocLL, $q) {
            // Creates a promise chain for each argument
            var promise = $q.when(1); // empty promise
            for(var i=0, len=_args.length; i < len; i ++){
                promise = andThen(_args[i]);
            }
            return promise;

            // creates promise to chain dynamically
            function andThen(_arg) {
                // also support a function that returns a promise
                if(typeof _arg == 'function')
                    return promise.then(_arg);
                else
                    return promise.then(function() {
                        // if is a module, pass the name. If not, pass the array
                        var whatToLoad = getRequired(_arg);
                        // simple error check
                        if(!whatToLoad) return $.error('Route resolve: Bad resource name [' + _arg + ']');
                        // finally, return a promise
                        return $ocLL.load( whatToLoad );
                    });
            }
            // check and returns required data
            // analyze module items with the form [name: '', files: []]
            // and also simple array of script files (for not angular js)
            function getRequired(name) {
                if (appRequires.modules)
                    for(var m in appRequires.modules)
                        if(appRequires.modules[m].name && appRequires.modules[m].name === name)
                            return appRequires.modules[m];
                return appRequires.scripts && appRequires.scripts[name];
            }

        }]};
    }; // resolveFor

    // not necessary, only used in config block for routes
    this.$get = function(){};

}]);

